(function (_super) {
    var RESTYPE_IMAGE = Laya.Loader.IMAGE;
    var RESTYPE_BUFFER = Laya.Loader.BUFFER;
    var RESTYPE_JSON = Laya.Loader.JSON;
    var RESTYPE_ATLAS = Laya.Loader.ATLAS;
    var GAME_ASSETS_LOADED = false;

    var assets = [];
    var io = ppt.io;

    function GameScene(data) {
        GameScene.super(this);

        this._data = null;
        this.uiPanel = null;
        this.gamePanel = null;

        this.ASSETS = {
            RES : ASSETS.GAME,
            TYPE : "normal"
        }
        
        this.init(data);
    }
    Laya.class(GameScene, "Scene.Game", _super);
    var _proto = GameScene.prototype;
    _proto.destroy = function () {
        _super.prototype.destroy.call(this, true);

        io.unregister("game::begin", this.checkAssetsLoaded);
        io.unregister("game::result", this.gameOver);
        
        this._data = null;
        this.uiPanel = null;
        this.gamePanel = null;
    }

    _proto.init = function (data) {
        console.log("GameScene Created. Assets:", assets);
        io.register("game::begin", this, this.checkAssetsLoaded);
        io.register("game::result", this, this.gameOver);

        this.size(GAME_CONFIG.WIDTH, Laya.stage.height);
        this._data = data;

        this.getRolesAssets(data);
        this.getMapAssets(data);
        this.getMonstersAssets(data);
        this.getTurretAssets(data);

        console.log("游戏页资源列表");
        for(var i in assets){
            console.log("URL:" + assets[i].url + ", TYPE:" + assets[i].type);
            Laya.URL.version[assets[i].url] = IMG_VERSION;
        }

        this.ASSETS.RES = this.ASSETS.RES.concat(assets);
    }
    _proto.getRolesAssets = function (data) {
        var roles = data.roles;

        for(var i in roles){
            var _role = roles[i];
            var roleSkin = _role.roleId;
            
            if(_role.avatar != ""){
                assets.push({
                    url : _role.avatar,
                    type : RESTYPE_IMAGE
                });
            }
            assets.push({
                url : ROLE_FIGHT_SKIN_PATH + roleSkin + ".json",
                type : RESTYPE_ATLAS
            });
            assets.push({
                url : ROLE_FIGHT_SKIN_PATH + roleSkin + "/act.sk",
                type : RESTYPE_BUFFER
            });
            assets.push({
                url : ROLE_FIGHT_SKIN_PATH + roleSkin + "/normal.sk",
                type : RESTYPE_BUFFER
            });
            assets.push({
                url : ROLE_FIGHT_SKIN_PATH + roleSkin + "/ride.sk",
                type : RESTYPE_BUFFER
            });
        }
    }
    _proto.getMapAssets = function (data) {
        var mapPath = data.map.path;

        assets.push({ url : mapPath + "/map.json", type : RESTYPE_JSON});
        assets.push({ url : mapPath + "/texture.png", type : RESTYPE_IMAGE});
        assets.push({ url : mapPath + "/bg.png", type : RESTYPE_IMAGE});
    }
    _proto.getMonstersAssets = function (data) {
        if(data.boss){
            assets.push({
                url : MONSTER_AVATAR_PATH + data.boss.skinId + ".png",
                type : RESTYPE_IMAGE
            });
            assets.push({
                url : MONSTER_SKIN_PATH + data.boss.skinId + ".png",
                type : RESTYPE_IMAGE
            });
            assets.push({
                url : MONSTER_SKIN_PATH + data.boss.skinId + ".sk",
                type : RESTYPE_BUFFER
            });

            if(data.guard){
                var guard = data.guard;
                for(var i in guard){
                    var guardSkin = guard[i];
                    
                    assets.push({
                        url : MONSTER_SKIN_PATH + guardSkin + ".png",
                        type : RESTYPE_IMAGE
                    });
                    assets.push({
                        url : MONSTER_SKIN_PATH + guardSkin + ".sk",
                        type : RESTYPE_BUFFER
                    });
                }
            }
        }
    }
    _proto.getTurretAssets = function (data) {
        var turretData = data.map.config.turret;

        if(turretData){
            assets.push({
                url : SPECIAL_SKIN_PATH + "turret.png",
                type : RESTYPE_IMAGE
            });
            assets.push({
                url : SPECIAL_SKIN_PATH + "turret.sk",
                type : RESTYPE_BUFFER
            });
        }
    }
    _proto.gameOver = function (data) {
        setTimeout(function () {
            console.log("游戏结束，跳转到结算页面");
            ppt.director.runScene(new Scene.Result(data));
        }, 4000);
    }
    _proto.checkAssetsLoaded = function (data) {
        if(!GAME_ASSETS_LOADED){
            io.unregister("game::begin", this.checkAssetsLoaded);

            alert("网络异常，请更换网络环境！");
            location.reload();
            return;
        }
    }
    
    _proto.onAdd = function () {
        GAME_ASSETS_LOADED = true;
            
        var ui = new Game.UI(this._data);
        var game = new Game.Main(this._data);

        this.uiPanel = ui;
        this.gamePanel = game;
        
        this.addChildren(game, ui);
    }
    _proto.onEnter = function () {
        this.uiPanel.enter();
        this.gamePanel.enter();
    }
    _proto.onExit = function () {
        this.destroy();

        for(var i in assets){
            Laya.loader.clearRes(assets[i].url, true);
        }
        console.log("GameScene Exit.");
        assets = [];
        GAME_ASSETS_LOADED = false;
    }
    _proto.onResize = function (height) {
        this.height = height;

        this.uiPanel.resize(height);
        this.gamePanel.resize(height);
    }
})(Laya.Sprite);